<?php
require_once './common.php';
/*
if (!defined('THINK_PATH')){
	exit('Access Denied');
}


//系统扩展方法
load('extend');

//以下是项目公共方法

// Check VerifyCode
 function VerifyCode()
 {
    if($_SESSION["verify"] != md5($_POST["verify"]))
    {
        return false;
    }
        return true;
  }

//SQL ADDSLASHES
function saddslashes($string) {
	if(is_array($string)) {
		foreach($string as $key => $val) {
			$string[$key] = saddslashes($val);
		}
	} else {
		$string = addslashes($string);
	}
	return $string;
}

//判断字符串是否存在
function strexists($haystack, $needle) {
	return !(strpos($haystack, $needle) === FALSE);
}

//获取用户信息
function getpassport($username,$password){
	$DbUser = M("User");
	//判断用户名是否存在
	$arrUser = $DbUser->where("user_email = '$username'")->find();
	$passwordsha1 = sha1($password);
	//var_dump($passwordsha1);
	//exit();
	if(empty($arrUser)){
		$status = -1;
	}elseif($arrUser['user_password'] != $passwordsha1){
		$status = -2;
	}else{
		$status = $arrUser;
	}
	$passport = $status;
	if($status > 0){
		$passport = array();
		$passport = $arrUser;
	}
	return $passport;
}

//判断当前用户登录状态
function checkauth() {
	global $_SGLOBAL;
	if(cookie('auth')){
		@list($password, $uid) = explode("\t", authcode(cookie('auth'), 'DECODE'));
		$_SGLOBAL['G_uid'] = intval($uid);
		if($password && $_SGLOBAL['G_uid']){
			$DbUser = M('User');
			$arrUser = array();
			$arrUser =  $DbUser->where("user_id = '".$_SGLOBAL[G_uid]."'")->find();
			if($arrUser){
				if($arrUser['user_password'] == $password){
					$_SGLOBAL['G_username'] = addslashes($arrUser['user_name']);
				}else{
					$_SGLOBAL['G_uid'] = 0;
				}
			}else{
				$_SGLOBAL['G_uid'] = 0;
			}
		}
	}
	
	if(empty($_SGLOBAL['G_uid'])){
		clearcookie();
	}
}

//清空cookie
function clearcookie(){
	global $_SGLOBAL;
	//清除当前前缀下的所有cookie
	cookie('auth',null);
	//全局变量置空
	$_SGLOBAL['G_uid'] = 0;
	$_SGLOBAL['G_username'] = '';

}

//SMTP 发送邮件
function SmtpSendMail($mailto,$subject,$body)
	{
		import("ORG.Net.Smtp");
		//$smtp = new Smtp();
		##########################################
		$smtpserver = C('smtp_server');//SMTP服务器
		$smtpserverport =C('smtp_serverport');//SMTP服务器端口
		$smtpusermail = C('smtp_usermail');//SMTP服务器的用户邮箱
		$smtpemailto = $mailto;//发送给谁
		$smtpuser = C('smtp_user');//SMTP服务器的用户帐号
		$smtppass = C('smtp_pass');//SMTP服务器的用户密码
		$mailsubject = $subject;//邮件主题
		$mailbody = $body;//邮件内容
		$mailtype = C('mail_type');//邮件格式（HTML/TXT）,TXT为文本邮件
		##########################################
		$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//这里面的一个true是表示使用身份验证,否则不使用身份验证.
		//$smtp->debug = TRUE;//是否显示发送的调试信息
		$smtp->debug = false;//是否显示发送的调试信息
		$smtp->sendmail($smtpemailto, $smtpusermail, $mailsubject, $mailbody, $mailtype);
		unset($smtp);
	}

//字符串解密加密
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {

	$ckey_length = 4;	// 随机密钥长度 取值 0-32;
				// 加入随机密钥，可以令密文无任何规律，即便是原文和密钥完全相同，加密结果也会每次不同，增大破解难度。
				// 取值越大，密文变动规律越大，密文变化 = 16 的 $ckey_length 次方
				// 当此值为 0 时，则不产生随机密钥
	$key = md5($key ? $key : C('BUYPG_KEY'));
	$keya = md5(substr($key, 0, 16));
	$keyb = md5(substr($key, 16, 16));
	$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';

	$cryptkey = $keya.md5($keya.$keyc);
	$key_length = strlen($cryptkey);

	$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
	$string_length = strlen($string);

	$result = '';
	$box = range(0, 255);

	$rndkey = array();
	for($i = 0; $i <= 255; $i++) {
		$rndkey[$i] = ord($cryptkey[$i % $key_length]);
	}

	for($j = $i = 0; $i < 256; $i++) {
		$j = ($j + $box[$i] + $rndkey[$i]) % 256;
		$tmp = $box[$i];
		$box[$i] = $box[$j];
		$box[$j] = $tmp;
	}

	for($a = $j = $i = 0; $i < $string_length; $i++) {
		$a = ($a + 1) % 256;
		$j = ($j + $box[$a]) % 256;
		$tmp = $box[$a];
		$box[$a] = $box[$j];
		$box[$j] = $tmp;
		$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
	}

	if($operation == 'DECODE') {
		if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
			return substr($result, 26);
		} else {
			return '';
		}
	} else {
		return $keyc.str_replace('=', '', base64_encode($result));
	}
}

//获取左侧分类，带缓存
function getLeftCategory(){
	if (S('leftcate')){
		$arrPrintCate = S('leftcate');
	} else{
		$DbCate = M("NbClass");
		$DbSite = M("NbSite");
		$arrResultCate = $DbCate->order('class_order desc')->select();
		foreach ($arrResultCate as $key=>$value){
			//查询当前分类下的10个网站按f7倒序
			$arrResultNbsite = array();
			$arrResultNbsite = $DbSite->where(' country_id = 0 and flag = 1 and class_id = '.$value['class_id'])->limit(10)->select();
			//格式化数据
			if($arrResultNbsite){
				foreach ($arrResultNbsite as $site_k => $site_v) {
					$arrResultNbsite[$site_k]['site_name'] = msubstr($site_v['site_name'], 0, 10, 'utf-8', FALSE);
					$arrResultNbsite[$site_k]['f7'] = formatFen($arrResultNbsite[$site_k]['f7']);
				}
			}
			$value['site'] = $arrResultNbsite;
			$arrPrintCate[$value['bigclass_id']][] = $value;
		}
		S('leftcate',$arrPrintCate,3600);
	}
	return $arrPrintCate;
}

//右侧专题 带缓存
function getRightZT(){
	if(S('rightzt')){
		$arrPrintZT = S('rightzt');
	}else{
		$DbZhuanTi = M('Zhuanti');
		$DbSite = M('NbSite');
		$arrResultZT = $DbZhuanTi->where('class_id = 0 and flag = 1')->order('zhuanti_id asc')->select();
		foreach ($arrResultZT as $key=>$value){
			$value['zhuanti_title'] = str_replace('网', '',$value['zhuanti_title']);
			$value['zhuanti_title'] .= "商城专题评价栏目";
			$arrResultNbsite = array();
			$arrResultNbsite = $DbSite->where('zhuanti_id = '.$value['zhuanti_id'])->order('site_id asc')->limit(10)->select();
			//格式化
			if($arrResultNbsite){
				foreach ($arrResultNbsite as $site_k => $site_v) {
					$arrResultNbsite[$site_k]['site_name'] = msubstr($site_v['site_name'], 0, 10, 'utf-8', FALSE);
					$arrResultNbsite[$site_k]['f7'] = formatFen($arrResultNbsite[$site_k]['f7']);
				}
			}
			$value['site'] = $arrResultNbsite;
			$arrPrintZT[] = $value;
		}
		S('rightzt' , $arrPrintZT, 3600);
	}
	return $arrPrintZT;
}

//分数格式化
function  formatFen($num){
	return $num = ($num % 10) ? number_format(floatval($num)/10, 1, '.', ' ') : $num/10;
}

//获取site_class的f1 至 f6的平均分
function getClassAvg($classid){
	$DbClass = M("ClassF");
	$arrClassAvg = $DbClass->field("f1_avg , f2_avg , f3_avg , f4_avg , f5_avg , f6_avg")->where("class_id = $classid")->find();
	return $arrClassAvg;
}

//获取site一个时段的f1 至 f6的平均分
function getSiteAvg($siteid,$sdate,$edate){
	$DbSite = M("NbF");
	if(empty($sdate) && empty($edate)){
		$where = "";
	}else{
		$andwhere = " and fdt > $sdate and fdt < $edate";
	}
	$arrSite = $DbSite->field("f1 , f2 , f3 , f4 , f5 , f6")->where("site_id = $siteid $andwhere")->select();
	$count = count($arrSite);
	if($arrSite){
		foreach ($arrSite as $key => $value){
			$avgf1 += $arrSite[$key]['f1'];
			$avgf2 += $arrSite[$key]['f2'];
			$avgf3 += $arrSite[$key]['f3'];
			$avgf4 += $arrSite[$key]['f4'];
			$avgf5 += $arrSite[$key]['f5'];
			$avgf6 += $arrSite[$key]['f6'];
		}
	}
	return array('f1' => $avgf1/$count, 'f2' => $avgf2/$count, 'f3' => $avgf3/$count, 'f4' => $avgf4/$count, 'f5' => $avgf5/$count, 'f6' => $avgf6/$count);
}

//生成近6个月的折线图数据， 带缓存
function getZheXian($siteid){
	if(S('zhexiantu_data_'.$siteid)){
		$LineData = S('zhexiantu_data_'.$siteid);
	}else{
		$ThisMon = strtotime("now");
		$LastMon = array();
		$arrAvg = array();
		for ($i = 1; $i <= 6; $i++){
			//格式化过去1到6个月时间
			$LastMon[$i] = strtotime("-".$i." month");
			//获取过去1到6个月时间的f1 - f6的分数（“月”下的f1至f6的值）
			if($i == 1){
				$arrAvg[$i] = getSiteAvg($siteid, $LastMon[$i], $ThisMon);
			}else{
				$arrAvg[$i] = getSiteAvg($siteid, $LastMon[$i], $LastMon[$i-1]);
			}
		}
		//格式化数据（“f”下的过去1月到过去6月的值）
		$LineData = array();
		foreach ($arrAvg as $key => $value){
			$LineData['f1'][] = $arrAvg[$key]['f1'] ? ($arrAvg[$key]['f1'] > 95 ? $arrAvg[$key]['f1'] + rand(-5,0) : $arrAvg[$key]['f1'] + rand(-5,5)) : rand(70,95);
			$LineData['f2'][] = $arrAvg[$key]['f2'] ? ($arrAvg[$key]['f2'] > 95 ? $arrAvg[$key]['f2'] + rand(-5,0) : $arrAvg[$key]['f2'] + rand(-5,5)) : rand(70,95); 
			$LineData['f3'][] = $arrAvg[$key]['f3'] ? ($arrAvg[$key]['f3'] > 95 ? $arrAvg[$key]['f3'] + rand(-5,0) : $arrAvg[$key]['f3'] + rand(-5,5)) : rand(70,95); 
			$LineData['f4'][] = $arrAvg[$key]['f4'] ? ($arrAvg[$key]['f4'] > 95 ? $arrAvg[$key]['f4'] + rand(-5,0) : $arrAvg[$key]['f4'] + rand(-5,5)) : rand(70,95); 
			$LineData['f5'][] = $arrAvg[$key]['f5'] ? ($arrAvg[$key]['f5'] > 95 ? $arrAvg[$key]['f5'] + rand(-5,0) : $arrAvg[$key]['f5'] + rand(-5,5)) : rand(70,95); 
			$LineData['f6'][] = $arrAvg[$key]['f6'] ? ($arrAvg[$key]['f6'] > 95 ? $arrAvg[$key]['f6'] + rand(-5,0) : $arrAvg[$key]['f6'] + rand(-5,5)) : rand(70,95); 
		}
		S('zhexiantu_data_'.$siteid , $LineData , 3600*24*30);
	}
	return $LineData;
}

//生成雷达图数据带缓存
function getLeiDai($siteid){
	if(S('leidatu_data_'.$siteid)){
		$LeiDaData = S('leidatu_data_'.$siteid);
	}else{
		$arrSiteAvg = getSiteAvg($siteid);
		$DbSite = M("NbSite");
		$arrSite = $DbSite->field("class_id")->where("site_id = $siteid")->find();
		$arrClassAvg = getClassAvg($arrSite['class_id']);
		//格式化数据
		foreach ($arrSiteAvg as $value){
			$resultSiteAvg[] = formatFen($value);
		}
		foreach ($arrClassAvg as $value){
			$resultClassAvg[] = formatFen($value);
		}
		$LeiDaData = array('website_avg' => $resultSiteAvg, 'class_avg' => $resultClassAvg);
		S('leidatu_data_'.$siteid , $LeiDaData , 3600*24*30);
	}
	return $LeiDaData;
}*/
?>
